<?php

namespace app\mini\controller;

use app\common\controller\Adminbase;
use app\model\Tutorial as TutorialModel;

class Tutorial extends Adminbase
{
    public function index()
    {
        $model = 'Tutorial';

        if ($this->request->isAjax()) {
            $map      = $this->buildParamsMap();
            $page     = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : 20;

            $list   = TutorialModel::where($map)
                ->field('*')
                ->with(['category'])
                ->withAttr('image', function ($value) {
                    return $value ? get_file_path($value, 1) : '';
                })
                ->withAttr('type', function ($value) {
                    return TutorialModel::TYPES[$value];
                })
                ->page($page, $pageSize)
                ->order('sort', 'asc')
                ->order('id', 'asc')
                ->select();
            $total  = TutorialModel::where($map)->count();
            $result = ["code" => 0, "count" => $total, "data" => $list];
            return json($result);
        }

        //生成列表数据
        $builder = new \app\common\controller\ListBuilder();
        $builder
            ->setDataUrl()
            ->setFormUrl('form')
            ->setLimit()
            ->setModel($model)
            ->addTableCheckBox()
            ->setSwitchTpl('switchTpl', 'status')
            ->setSwitchTpl('amplifyTpl', 'image')
            ->addTableColumn('id', 'ID', null, null, true)
            ->addTableColumn('category_name', '分类')
            ->addTableColumn('title', '标题')
            ->addTableColumn('image', '教程卡片', null, '#amplifyTpl')
            ->addTableColumn('type', '教程播放方式')
            ->addTableColumn('sort', '排序', null, null, false, '', false, '', true)
            ->addTableOperation()
            ->returnForm();

        $this->assign('module', 'app');
        return $this->fetch('admin@common/lists');
    }

    public function form()
    {
        $model = 'Tutorial';

        $id = $this->request->param('id/d');
        if (isset($id) && $id > 0) {
            $info = TutorialModel::get($id);
            if ($info['type'] == 1) {
                $info['video_url'] = $info['value'];
            } elseif ($info['type'] == 2) {
                $info['image_url'] = $info['value'];
            }
            $this->assign('id', $id);
            $this->assign('action', 2);
        } else {
            $info = [
                'sort' => 50,
                'type' => 1,
            ];
        }

        if ($this->request->isPost()) {
            $data   = $this->request->post();
            $module = $data['module'] ?: request()->module();
            $model  = $data['model'];
            $type   = $data['type'];
            if ($type == 3) {
                $data['value'] = '';
            } else {
                $data['value']       = $type == 1 ? $data['video_url'] : $data['image_url'];
                $data['video_appid'] = $data['video_path'] = '';
            }

            $data_model = 'app\\' . '\\model' . '\\' . $model;
            if ($data['action'] == 1) {
                unset($data['id']);
                $res = TutorialModel::create($data);
            } else {
                $res = saveDataByModel(TutorialModel::class, $data, ['id' => $data['id']]);
            }
            if ($res) {
                $result = ['code' => 0, 'msg' => '操作成功'];
            } else {
                $result = ['code' => -1, 'msg' => '操作失败'];
            }
            return json($result);
        }

        $category = getModelList('TutorialCategory', 'id, name', 'name', ['status' => 1]);
        $builder  = new \app\common\controller\FormBuilder();
        $builder
            ->setUrl('form')
            ->setModel($model)
            ->setSuccessUrl()
            ->addFormItem('category_id', 'select', '分类', '', $category, 1)
            ->addFormItem('title', 'text', '标题', '前端不展示', [], 1)
            ->addFormItem('image', 'image', '教程卡片', '', [], 1)
            ->addFormItem('type', 'radio', '教程播放方式', '', TutorialModel::TYPES, 1)
            ->addFormItem('video_url', 'file', '视频文件')
            ->addFormItem('image_url', 'image', '图片文件')
            ->addFormItem('video_appid', 'text', '视频号id')
            ->addFormItem('video_path', 'text', '视频号链接')
            ->addFormItem('sort', 'gtnumber', '排序', '值越小排序越靠前')
            ->setFormData($info)
            ->setExtraHtml($this->extra_html)
            ->returnForm();
        $this->assign('module', 'app');
        $this->assign('is_verify', 0);
        return $this->fetch('admin@common/form');
    }

    private $extra_html = <<<EOF
    <script type="text/javascript">
        $(function(){
            layui.use(['form'], function () {
                var form = layui.form;
                //获取单选框name=qualification_type选择的值
                $('input[name="type"]').each(function(){
                    if ($(this).is(':checked')) {
                        handleTypeOption($(this).val())
                    }
                })
                form.on('radio(type)', function(data){
                    handleTypeOption(data.value)
                });
                function handleTypeOption(type) {
                    switch (parseInt(type)) {
                        case 1:
                            $('.item-video_url').removeClass('layui-hide');
                            $('.item-image_url').addClass('layui-hide');
                            $('.item-video_appid').addClass('layui-hide');
                            $('.item-video_path').addClass('layui-hide');
                            break;
                        case 2:
                            $('.item-image_url').removeClass('layui-hide');
                            $('.item-video_url').addClass('layui-hide');
                            $('.item-video_appid').addClass('layui-hide');
                            $('.item-video_path').addClass('layui-hide');
                            break;
                        case 3:
                            $('.item-video_appid').removeClass('layui-hide');
                            $('.item-video_path').removeClass('layui-hide');
                            $('.item-video_url').addClass('layui-hide');
                            $('.item-image_url').addClass('layui-hide');
                            break;    
                    }
                    
                } 
            });
        });
    </script>
EOF;
}
